<?php
/*
	本插件：$this->model('sheyu')->方法名();
	其他地方调用：model('jy_aixiaoyi/sheyu')->方法名();
	http://www.aibayes.cn/bes-project/api-document.html
*/
namespace jy_aixiaoyi;
defined('BY_JYA') or exit('error');
class sheyu extends \plugin{
	//发起http请求
	public function request($rurl,$post=null,$isres=false){
		$basic=$this->plugin_setting('basic');
		if(!$basic['sz_appid'] || !$basic['sz_apisecret']){
			exi('暂时无法使用，请联系管理员，设置好基本参数','error');
		}
		$this->she_time($basic);
		$post['app_id']=$basic['sz_appid'];
		$post['timestamp']=date('YmdHis');
		$restime =strtoupper(md5($post['timestamp']));
		$post['sign']=md5($restime.$basic['sz_apisecret']);
		$post['sign']=strtoupper($post['sign']);
		//print_r('~~'.ijson_encode($post));exit;
		//$post=ijson_encode($post);
		$result=cfc('http')->ihttp_request($rurl,$post);
		if(is_error($result)){			
			$message="访问失败, 错误: {$result['message']}";
			if($return_error){
				return error($message);
			}else{
				exi($message,'error');
			}
		}
		$data=preg_replace('/[\x00-\x1F]/','',$result['content']);
		$data=@json_decode($data,true);
		if($data['errcode']){
		}
		if($direct){
			return $result;
		}
		if(!is_array($data) || !$data){
			$message='访问接口错误，以下为原始数据：'.$result['content'];
			if($return_error){
				return error($message);
			}else{
				exi($message,'error');
			}
		}
		if($data['code']!=200){
			exi('请求不成功！'.$data['msg'].$post['method'],'error');
		}
		$this->bi_sheyu=$basic['bi_sheyu'];
		return $data['data'];
	}
	public function she_time($basic=array()){
		$basic=$basic?:$this->plugin_setting('basic');
		if($basic['sheyue_time']){
			$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
			$msgnum1=pdo_count('jy_aixiaoyi_shelog',array('createtime >'=>$daytime,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
			$msgnum2=pdo_count('jy_aixiaoyi_mianlog',array('createtime >'=>$daytime,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));	
			$msgnum3=pdo_count('jy_aixiaoyi_tilog',array('createtime >'=>$daytime,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
			$msgnum4=pdo_count('jy_aixiaoyi_bianlog',array('createtime >'=>$daytime,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));			
			$msgnum=$msgnum1+$msgnum2+$msgnum3+$msgnum4;
			if($basic['sheyue_time']<=$msgnum){
				exi('当前使用次数已用完！！','error');
			}
		}
		$checkvip=$this->model('member')->checkvip('','',1);
		if($checkvip!='isvip'){
			if($basic['bi_open'] && $basic['bi_sheyu']>0){
				$member=model('core/member')->detail($_SESSION['uid'],array('credit1','credit2','credit3','credit4','credit6','mobile','avatar','nickname'));
				if($basic['bi_sheyu'] >0 && $basic['bi_sheyu']>$member['credit3']){
					exi('金币不足！！不能使用！','error');
				}
			}
		}
		return '';
	}
	//舌诊证型辨识接口
	public function getmsg($data=array(),$method='') {
		$isget=pdo_get('jy_aixiaoyi_shelog',array('uniacid'=>$_SESSION['uniacid'],'image'=>$data['image']));
		if(!$isget){
			$url = "http://aibayes.cn/api/analysis";
			$post=array(
				'imgpath'=>tomedia($data['image']),
				'method'=>$method,
				'timeout_express'=>100,
				'version'=>'1.0',
			);
			$res=$this->request($url,$post);
			$res=json_decode($res,true);
		}else{
			$res=json_decode($isget['content'],true);
		}
		//file_put_contents(ROOT_D.'/1.txt',ijson_encode($res));
		//print_r($res);exit;
		$id=$this->upshelog($res,$data);
		if($res['code']!='88'){
			$res['message']=$res['message']?:$res['errMsg'];
			exi('识别不成功！'.$res['message'],'error');
		}
		return $id;
	}
	//面诊接口
	public function getmian($data=array(),$method='') {
		$isget=pdo_get('jy_aixiaoyi_mianlog',array('uniacid'=>$_SESSION['uniacid'],'image'=>$data['image']));
		if(!$isget){
			$url = "http://aibayes.cn/api/analysis";
			$post=array(
				'imgpath'=>tomedia($data['image']),
				'method'=>$method,
				'timeout_express'=>100,
				'version'=>'1.0',
			);
			$res=$this->request($url,$post);
			$res=json_decode($res,true);
		}else{
			$res=json_decode($isget['content'],true);
		}
		//file_put_contents(ROOT_D.'/1.txt',ijson_encode($res));
		//print_r($res);exit;
		$id=$this->upmianlog($res,$data);
		if($res['code']!='88'){
			$res['message']=$res['message']?:$res['errMsg'];
			exi('识别不成功！'.$res['message'],'error');
		}
		return $id;
	}
	//舌面诊-证型+体质辨识接口
	public function getti($data=array(),$method='',$isshe='') {
	    if($isshe){
	       $method=$isshe;
	    }
		$isget=pdo_get('jy_aixiaoyi_tilog',array('uniacid'=>$_SESSION['uniacid'],'image'=>$data['image'],'image2'=>$data['image2']));
		if(!$isget){
			$url = "http://aibayes.cn/api/analysis";
			if($isshe){
				$post=array(
					'imgpath'=>tomedia($data['image']),
					'method'=>$isshe,
					'timeout_express'=>100,
					'version'=>'1.0',
				);
			}else{
				$post=array(
					'TongueImgpath'=>tomedia($data['image']),
					'FaceImgpath'=>tomedia($data['image2']),
					'method'=>$method,
					'timeout_express'=>100,
					'version'=>'1.0',
				);
			}
			$res=$this->request($url,$post);
			$res=json_decode($res,true);
		}else{
			$res=json_decode($isget['content'],true);
		}
		//file_put_contents(ROOT_D.'/1.txt',ijson_encode($res));
		//print_r($res);exit;
		$id=$this->uptilog($res,$data);
		if($res['code']!='88' && $res['code']!='8888'){
			$res['message']=$res['message']?:$res['errMsg'];
			exi('识别不成功！'.$res['code'].$res['message'].$method,'error');
		}
		return $id;
	}
	public function getbian($data=array(),$method='') {
		$isget=pdo_get('jy_aixiaoyi_bianlog',array('uniacid'=>$_SESSION['uniacid'],'image'=>$data['image'],'image2'=>$data['image2']));
		if(!$isget){
			$url = "http://aibayes.cn/api/analysis";
			$post=array(
				'TongueImgpath'=>tomedia($data['image']),
				'FaceImgpath'=>tomedia($data['image2']),
				'method'=>$method,
				'timeout_express'=>100,
				'version'=>'1.0',
			);
			$res=$this->request($url,$post);
			$res=json_decode($res,true);
		}else{
			$res=json_decode($isget['content'],true);
		}
		//file_put_contents(ROOT_D.'/1.txt',ijson_encode($res));
		//print_r($res);exit;
		$id=$this->upbianlog($res,$data);
		if($res['code']!='88'){
			$res['message']=$res['message']?:$res['errMsg'];
			exi('识别不成功！'.$res['message'],'error');
		}
		return $id;
	}
	public function upshelog($content=array(),$postdata=array()){
		if($content['cutTongue']){
			$image=@file_get_contents($content['cutTongue']);
			$ext=substr(strrchr($content['cutTongue'],'.'),1);
			$content['cutTonguenew']=cfc('files')->source_save($image,'image',$ext);
		}
		$postdata['guwenid']=$postdata['guwenid']?:pdo_getcolumn('jy_aixiaoyi_member',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'guwenid');
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'guwenid'=>$postdata['guwenid'],
			'image'=>$postdata['image'],
			'content'=>ijson_encode($content),
			'status'=>1,
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_aixiaoyi_shelog',$data);
		$id=pdo_insertid();
		$checkvip=$this->model('member')->checkvip('',true,1);
		if($checkvip!='isvip' && $this->bi_sheyu){
			$isgetfee=pdo_get('core_credit_log',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'plugin'=>'jy_aixiaoyi','tag'=>'bi_sheyu'.$id),'id');
			if(!$isgetfee){
				model('core/member')->member_credit($_SESSION['uid'],'credit3',$this->bi_sheyu,false,'jy_aixiaoyi','舌面诊消耗','bi_sheyu'.$id);
			}
		}
		return $id;
	}
	public function upmianlog($content=array(),$postdata=array()){
		if($content['cutTongue']){
			$image=@file_get_contents($content['cutTongue']);
			$ext=substr(strrchr($content['cutTongue'],'.'),1);
			$content['cutTonguenew']=cfc('files')->source_save($image,'image',$ext);
		}
		$postdata['guwenid']=$postdata['guwenid']?:pdo_getcolumn('jy_aixiaoyi_member',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'guwenid');
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'guwenid'=>$postdata['guwenid'],
			'image'=>$postdata['image'],
			'content'=>ijson_encode($content),
			'status'=>1,
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_aixiaoyi_mianlog',$data);
		$id=pdo_insertid();
		$checkvip=$this->model('member')->checkvip('',true,1);
		if($checkvip!='isvip' && $this->bi_sheyu){
			$isgetfee=pdo_get('core_credit_log',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'plugin'=>'jy_aixiaoyi','tag'=>'bi_sheyum'.$id),'id');
			if(!$isgetfee){
				model('core/member')->member_credit($_SESSION['uid'],'credit3',$this->bi_sheyu,false,'jy_aixiaoyi','舌面诊消耗','bi_sheyum'.$id);
			}
		}
		return $id;
	}
	public function uptilog($content=array(),$postdata=array()){
		if($content['cutTongue']){
			$image=@file_get_contents($content['cutTongue']);
			$ext=substr(strrchr($content['cutTongue'],'.'),1);
			$content['cutTonguenew']=cfc('files')->source_save($image,'image',$ext);
		}
		$postdata['guwenid']=$postdata['guwenid']?:pdo_getcolumn('jy_aixiaoyi_member',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'guwenid');
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'guwenid'=>$postdata['guwenid'],
			'image'=>$postdata['image'],
			'image2'=>$postdata['image2'],
			'content'=>ijson_encode($content),
			'status'=>1,
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_aixiaoyi_tilog',$data);
		$id=pdo_insertid();
		$checkvip=$this->model('member')->checkvip('',true,1);
		if($checkvip!='isvip' && $this->bi_sheyu){
			$isgetfee=pdo_get('core_credit_log',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'plugin'=>'jy_aixiaoyi','tag'=>'bi_sheyut'.$id),'id');
			if(!$isgetfee){
				model('core/member')->member_credit($_SESSION['uid'],'credit3',$this->bi_sheyu,false,'jy_aixiaoyi','舌面诊消耗','bi_sheyut'.$id);
			}
		}
		return $id;
	}
	public function upbianlog($content=array(),$postdata=array()){
		if($content['cutTongue']){
			$image=@file_get_contents($content['cutTongue']);
			$ext=substr(strrchr($content['cutTongue'],'.'),1);
			$content['cutTonguenew']=cfc('files')->source_save($image,'image',$ext);
		}
		$postdata['guwenid']=$postdata['guwenid']?:pdo_getcolumn('jy_aixiaoyi_member',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'guwenid');
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'guwenid'=>$postdata['guwenid'],
			'image'=>$postdata['image'],
			'image2'=>$postdata['image2'],
			'content'=>ijson_encode($content),
			'status'=>1,
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_aixiaoyi_bianlog',$data);
		$id=pdo_insertid();
		$checkvip=$this->model('member')->checkvip('',true,1);
		if($checkvip!='isvip' && $this->bi_sheyu){
			$isgetfee=pdo_get('core_credit_log',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'plugin'=>'jy_aixiaoyi','tag'=>'bi_sheyuti'.$id),'id');
			if(!$isgetfee){
				model('core/member')->member_credit($_SESSION['uid'],'credit3',$this->bi_sheyu,false,'jy_aixiaoyi','舌面诊消耗','bi_sheyuti'.$id);
			}
		}
		return $id;
	}
}
?>